// function getElementPath(element) {
//     let path = [];
//     while (element.parentElement) {
//         let selector;
//         if (element.id) {
//             selector = `#${element.id}`;
//         } else {
//             let siblings = Array.from(element.parentElement.children).filter(sibling => sibling.tagName.toLowerCase() === element.tagName.toLowerCase());
//             let index = siblings.indexOf(element) + 1;
//             selector = element.tagName.toLowerCase();
//             if (siblings.length > 1) {
//                 selector += `[${index}]`;
//             }
//         }
//         path.unshift(selector);
//         element = element.parentElement;
//     }
//     path.unshift('html');
//     return path.join(' > ');
// }



/* 获取all */
function getElementPath(element) {
    let tagPath = [];
    let idPath = [];
    let classPath = [];

    while (element.parentElement) {
        // 获取标签名路径
        let tagSelector;
        let siblings = Array.from(element.parentElement.children).filter(sibling => sibling.tagName.toLowerCase() === element.tagName.toLowerCase());
        let index = siblings.indexOf(element) + 1;
        tagSelector = element.tagName.toLowerCase();
        if (siblings.length > 1) {
            tagSelector += `[${index}]`;
        }
        tagPath.unshift(tagSelector);

        // 获取 ID 路径
        if (element.id) {
            idPath.unshift(`#${element.id}`);
        } else {
            idPath.unshift('#');
        }

        // 获取类名路径
        if (element.className) {
            classPath.unshift(`.${element.className.split(' ').join('.')}`);
        } else {
            classPath.unshift('.');
        }

        element = element.parentElement;
    }

    // 添加顶级 html
    tagPath.unshift('html');
    idPath.unshift('html');
    classPath.unshift('html');

    return {
        tagPath: tagPath.join(' > '),
        idPath: idPath.join(' > '),
        classPath: classPath.join(' > ')
    };
}



document.addEventListener('mouseover', function (e) {
    e.stopPropagation();
    e.preventDefault();

    let el = e.target;
    el.classList.add('highlight');

    // let path = getElementPath(el); 
    // console.log(path);
    // chrome.runtime.sendMessage({ type: 'updatePath', path: path });

    el.addEventListener('mouseout', function () {
        el.classList.remove('highlight');
    }, { once: true });
});




/* 点击事件  获取元素 */
document.addEventListener('click', function (e) {
    e.stopPropagation();
    e.preventDefault();
    let el = e.target;
    let path = getElementPath(el);
    console.log(path);
    chrome.runtime.sendMessage({ type: 'updatePath', path: path });

});
